/********************************** (C) COPYRIGHT *******************************
 * File Name          : RGB1W.c
 * Author             : WCH
 * Version            : V1.0.1
 * Date               : 2024/06/05
 * Description        : 1-wire example 1W-RGB, 1W-DS1820
 *********************************************************************************
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * Attention: This software (modified or not) and binary are used for
 * microcontroller manufactured by Nanjing Qinheng Microelectronics.
 *******************************************************************************/

const unsigned char PIOC_1W_CODE[] =
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x16,0x60,0x00,0x00,0x00,0x00,0x00,0x00,   /* .........`...... */
 0x00,0x00,0x0A,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,   /* ...p............ */
 0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x1C,0x5E,0x1C,0x47,   /* ......0......^.G */
 0x1C,0x5D,0x16,0x60,0x1C,0x47,0x1E,0x02,0x09,0x10,0x33,0xC1,0x40,0xC2,0x36,0xC3,   /* .].`.G....3.@.6. */
 0x31,0x80,0x20,0x2F,0x43,0x38,0x60,0x2F,0x95,0x38,0x87,0x2F,0x31,0x38,0xFC,0x2F,   /* 1...C8`..8..18.. */
 0xE4,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,   /* .8.............. */
 0x00,0x00,0x01,0x28,0x90,0x60,0x00,0x00,0x22,0x72,0x90,0x60,0x00,0x00,0x22,0x72,   /* ...(.`.."r.`.."r */
 0x90,0x30,0x4B,0x24,0x28,0x22,0xDF,0x71,0x04,0x15,0x3B,0x30,0x09,0x15,0x3A,0x30,   /* .0K$(".q..;0..:0 */
 0x00,0x00,0x2E,0x72,0x90,0x60,0x00,0x00,0x08,0x01,0x0B,0x40,0x0A,0x48,0x0C,0x01,   /* ...r.`.....@.H.. */
 0x06,0x28,0x0B,0x54,0x90,0x60,0x09,0x02,0x04,0x10,0x20,0x24,0x00,0x00,0x0B,0x48,   /* .(.T.`.....$...H */
 0x0B,0x70,0x01,0x5F,0x0B,0x40,0x08,0x70,0x0B,0x40,0x0A,0x70,0x0B,0x48,0x0B,0x70,   /* .p._.@.p.@.p.H.p */
 0x01,0x5E,0x0B,0x40,0x08,0x70,0x0B,0x40,0x0A,0x70,0x0B,0x48,0x0B,0x70,0x01,0x5D,   /* .^.@.p.@.p.H.p.] */
 0x0B,0x40,0x08,0x70,0x0B,0x40,0x0A,0x70,0x0B,0x48,0x0B,0x70,0x01,0x5C,0x0B,0x40,   /* .@.p.@.p.H.p.\.@ */
 0x08,0x70,0x0B,0x40,0x0A,0x70,0x0B,0x48,0x0B,0x70,0x01,0x5B,0x0B,0x40,0x08,0x70,   /* .p.@.p.H.p.[.@.p */
 0x0B,0x40,0x0A,0x70,0x0B,0x48,0x0B,0x70,0x01,0x5A,0x0B,0x40,0x08,0x70,0x0B,0x40,   /* .@.p.H.p.Z.@.p.@ */
 0x0A,0x70,0x0B,0x48,0x0B,0x70,0x01,0x59,0x0B,0x40,0x08,0x70,0x0B,0x40,0x0A,0x70,   /* .p.H.p.Y.@.p.@.p */
 0x0B,0x48,0x0B,0x70,0x01,0x58,0x0B,0x40,0x09,0x70,0x01,0x02,0x0B,0x40,0x0D,0x70,   /* .H.p.X.@.p...@.p */
 0x04,0x15,0x4F,0x30,0x1E,0x28,0xE4,0x71,0xE4,0x71,0x04,0x00,0x0B,0x54,0x04,0x28,   /* ..O0.(.q.q...T.( */
 0x1D,0x10,0x1C,0x4F,0x16,0x60,0x02,0x28,0x90,0x60,0x00,0x00,0x09,0x46,0x08,0x01,   /* ...O.`.(.`...F.. */
 0x0B,0x41,0x0A,0x49,0x0C,0x01,0x06,0x28,0x0B,0x55,0x90,0x60,0x09,0x02,0x04,0x10,   /* .A.I...(.U.`.... */
 0x20,0x24,0x00,0x00,0x0B,0x49,0x0B,0x70,0x01,0x5F,0x0B,0x41,0x08,0x70,0x0B,0x41,   /* .$...I.p._.A.p.A */
 0x0A,0x70,0x0B,0x49,0x0B,0x70,0x01,0x5E,0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,   /* .p.I.p.^.A.p.A.p */
 0x0B,0x49,0x0B,0x70,0x01,0x5D,0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,   /* .I.p.].A.p.A.p.I */
 0x0B,0x70,0x01,0x5C,0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,0x0B,0x70,   /* .p.\.A.p.A.p.I.p */
 0x01,0x5B,0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,0x0B,0x70,0x01,0x5A,   /* .[.A.p.A.p.I.p.Z */
 0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,0x0B,0x70,0x01,0x59,0x0B,0x41,   /* .A.p.A.p.I.p.Y.A */
 0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,0x0B,0x70,0x01,0x58,0x0B,0x41,0x09,0x70,   /* .p.A.p.I.p.X.A.p */
 0x01,0x02,0x0B,0x41,0x0D,0x70,0x04,0x15,0xA2,0x30,0x1E,0x28,0xE4,0x71,0xE4,0x71,   /* ...A.p...0.(.q.q */
 0x04,0x00,0x0B,0x55,0x04,0x28,0x90,0x60,0x00,0x00,0x22,0x50,0x3C,0x61,0x08,0x01,   /* ...U.(.`.."P<a.. */
 0x0B,0x40,0x80,0x28,0x0C,0x10,0x20,0x02,0x21,0x0A,0x93,0x34,0x0A,0x48,0x09,0x47,   /* .@.(....!..4.H.G */
 0x3E,0x01,0x04,0x01,0x06,0x28,0x0B,0x54,0x90,0x60,0x02,0x28,0x3F,0x10,0x18,0x00,   /* >....(.T.`.(?... */
 0x1F,0x10,0x80,0x29,0x09,0x0A,0x00,0x00,0x08,0x10,0x13,0x00,0xAE,0x00,0x13,0x00,   /* ...)............ */
 0xAD,0x00,0x13,0x00,0xAC,0x00,0x13,0x00,0xAB,0x00,0x13,0x00,0xAA,0x00,0x13,0x00,   /* ................ */
 0x20,0x15,0x20,0x04,0x03,0x52,0x21,0x15,0xA9,0x00,0x13,0x00,0x20,0x02,0x21,0x0A,   /* .....R!.......!. */
 0x36,0x35,0xA8,0x00,0x13,0x00,0x04,0x02,0x1F,0x10,0xAF,0x00,0x13,0x00,0xAE,0x00,   /* 65.............. */
 0x13,0x00,0xAD,0x00,0x13,0x00,0xAC,0x00,0x13,0x00,0x20,0x15,0x20,0x04,0x03,0x52,   /* ...............R */
 0x21,0x15,0xAB,0x00,0x13,0x00,0x3E,0x14,0x03,0x52,0x3F,0x14,0xAA,0x00,0x13,0x00,   /* !.....>..R?..... */
 0x3E,0x02,0x04,0x10,0xA9,0x00,0x13,0x00,0x20,0x02,0x21,0x0A,0x36,0x35,0x3F,0x02,   /* >.........!.65?. */
 0xA8,0x00,0x13,0x00,0x18,0x00,0x1F,0x10,0xAF,0x00,0xFD,0x60,0xA8,0x00,0x13,0x00,   /* ...........`.... */
 0x08,0x47,0x12,0x00,0x08,0x01,0x8A,0x60,0x00,0x00,0x08,0x01,0x0C,0x01,0x0B,0x41,   /* .G.....`.......A */
 0x20,0x02,0x21,0x0A,0x93,0x34,0x0A,0x49,0x3E,0x01,0x04,0x01,0x06,0x28,0x0B,0x54,   /* ..!..4.I>....(.T */
 0x90,0x60,0x02,0x28,0x3F,0x10,0x18,0x00,0x1F,0x10,0x0B,0x49,0x0B,0x70,0x1F,0x5F,   /* .`.(?......I.p._ */
 0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,0x0B,0x70,0x1F,0x5E,0x0B,0x41,   /* .A.p.A.p.I.p.^.A */
 0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,0x0B,0x70,0x1F,0x5D,0x0B,0x41,0x08,0x70,   /* .p.A.p.I.p.].A.p */
 0x0B,0x41,0x0A,0x70,0x0B,0x49,0x0B,0x70,0x1F,0x5C,0x0B,0x41,0x08,0x70,0x0B,0x41,   /* .A.p.I.p.\.A.p.A */
 0x0A,0x70,0x0B,0x49,0x0B,0x70,0x1F,0x5B,0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,   /* .p.I.p.[.A.p.A.p */
 0x0B,0x49,0x0B,0x70,0x1F,0x5A,0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,   /* .I.p.Z.A.p.A.p.I */
 0x0B,0x70,0x1F,0x59,0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,0x0B,0x70,   /* .p.Y.A.p.A.p.I.p */
 0x1F,0x58,0x0B,0x41,0x08,0x70,0x0B,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,   /* .X.A.p.A........ */
 0x20,0x15,0x20,0x04,0x03,0x52,0x21,0x15,0x20,0x02,0x21,0x0A,0xDD,0x34,0x04,0x02,   /* .....R!...!..4.. */
 0x1F,0x10,0x0B,0x49,0x0B,0x70,0x1F,0x5F,0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,   /* ...I.p._.A.p.A.p */
 0x0B,0x49,0x0B,0x70,0x1F,0x5E,0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,   /* .I.p.^.A.p.A.p.I */
 0x0B,0x70,0x1F,0x5D,0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,0x0B,0x70,   /* .p.].A.p.A.p.I.p */
 0x1F,0x5C,0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,0x0B,0x70,0x1F,0x5B,   /* .\.A.p.A.p.I.p.[ */
 0x0B,0x41,0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,0x0B,0x70,0x1F,0x5A,0x0B,0x41,   /* .A.p.A.p.I.p.Z.A */
 0x08,0x70,0x0B,0x41,0x0A,0x70,0x0B,0x49,0x0B,0x70,0x1F,0x59,0x0B,0x41,0x08,0x70,   /* .p.A.p.I.p.Y.A.p */
 0x0B,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x15,0x20,0x04,0x03,0x52,   /* .A.............R */
 0x21,0x15,0x3E,0x14,0x03,0x52,0x3F,0x14,0x3E,0x02,0x04,0x10,0x0B,0x49,0x0B,0x70,   /* !.>..R?.>....I.p */
 0x1F,0x58,0x0B,0x41,0x08,0x70,0x0B,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,   /* .X.A.p.A........ */
 0x20,0x02,0x21,0x0A,0xDD,0x34,0x3F,0x02,0x18,0x00,0x1F,0x10,0x4D,0x61,0xFA,0x28,   /* ..!..4?.....Ma.( */
 0xE4,0x61,0x41,0x28,0xE4,0x61,0x02,0x28,0x0D,0x70,0x00,0x00,0x0D,0x70,0x00,0x00,   /* .aA(.a.(.p...p.. */
 0x0D,0x70,0x00,0x00,0x0D,0x70,0x00,0x00,0xFF,0x2C,0x00,0x00,0xE4,0x31,0x30,0x00,   /* .p...p...,...10. */
 0x0A,0x40,0x08,0x01,0x0C,0x01,0x05,0x28,0xE4,0x71,0x0B,0x40,0x0A,0x48,0xDF,0x71,   /* .@.....(.q.@.H.q */
 0xDF,0x71,0x0A,0x40,0xE1,0x71,0x09,0x01,0x0B,0x54,0x04,0x24,0xDF,0x71,0x09,0x02,   /* .q.@.q...T.$.q.. */
 0x30,0x00,0x09,0x10,0x08,0x22,0x0B,0x40,0x0A,0x48,0xE3,0x71,0x09,0x50,0x0A,0x40,   /* 0....".@.H.q.P.@ */
 0xE1,0x71,0x0A,0x40,0x05,0x28,0x09,0x58,0xE4,0x71,0x09,0x1F,0x04,0x15,0x03,0x32,   /* .q.@.(.X.q.....2 */
 0x30,0x00,0x08,0x22,0x0B,0x40,0x0A,0x48,0xE3,0x71,0x0A,0x40,0x0A,0x28,0xE4,0x71,   /* 0..".@.H.q.@.(.q */
 0x09,0x1F,0x09,0x47,0x0B,0x54,0x09,0x4F,0x37,0x28,0xE4,0x71,0x04,0x15,0x12,0x32,   /* ...G.T.O7(.q...2 */
 0x09,0x02,0x30,0x00,0xF0,0x71,0x2D,0x32,0xCC,0x28,0x01,0x72,0x44,0x28,0x01,0x72,   /* ..0..q-2.(.rD(.r */
 0x1C,0x5C,0x2C,0x62,0x0B,0x48,0x0A,0x48,0x04,0x00,0x30,0x00,0xF0,0x71,0x39,0x32,   /* .\,b.H.H..0..q92 */
 0xCC,0x28,0x01,0x72,0xBE,0x28,0x01,0x72,0x11,0x72,0x20,0x10,0x11,0x72,0x21,0x10,   /* .(.r.(.r.r...r!. */
 0xF0,0x71,0x30,0x00,0x00,0x00};    /* .q0... */


#include "RGB1W.h"
#include <string.h>

__IO  uint8_t	stat;

void PIOC_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

/*********************************************************************
 * @fn      PIOC_IRQHandler
 *
 * @brief   This function handles PIOC exception.
 *
 * @return  none
 */
void PIOC_IRQHandler( void )
{
//	uint8_t	stat;
	stat = PIOC->D8_CTRL_RD;//auto remove interrupt request after reading
//	if ( stat == RGB1W_ERR_OK ) printf("1-wire finished\r\n");
//	else printf("1-wire error %02x\r\n", stat);
//	temper = PIOC->D16_DATA_REG0_1;//for DS1820 only
}

/*********************************************************************
 * @fn      RGB1W_Init
 *
 * @brief   Init RGB1W.
 *
 * @return  none
 */
void RGB1W_Init ( void ) {
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_IO2W, ENABLE);

#if 1  //PC18 PC19
    GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
    GPIO_SetBits(GPIOC, GPIO_Pin_18);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_18|GPIO_Pin_19;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

#else  //PC7
    GPIO_PinRemapConfig(GPIO_Remap_PIOC, ENABLE);
    GPIO_SetBits(GPIOC, GPIO_Pin_7);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
#endif

//	PIOC->D8_SYS_CFG = 0;//halt
	PIOC->D8_SYS_CFG = RB_MST_RESET | RB_MST_IO_EN0| RB_MST_IO_EN1;	// reset PIOC & enable IO0 IO1

	memcpy( (uint8_t *)(PIOC_SRAM_BASE), PIOC_1W_CODE, sizeof( PIOC_1W_CODE ) );	// load code for PIOC

#if defined SYSCLK_FREQ_24MHz_HSI
	*(uint32_t)(PIOC_SRAM_BASE+PIOC_FREQ_CFG) = 0x00300030;	// set if Fsys=24MHz, default Fsys=48MHz
#else	// default is 48MHz

#endif

//    NVIC_EnableIRQ(PIOC_IRQn);//enable interrupt
//    NVIC_SetPriority(PIOC_IRQn,0xf0);
}

/*********************************************************************
 * @fn      RGB1W_SendSFR
 *
 * @brief   SFR mode for 1~32 bytes data.
 *
 * @param   total_bytes - total data number(byte).
 *          p_source_addr - data.
 *          mod - 0:PC18 or PC7
 *                1:PC19
 *
 * @return  none
 */
void RGB1W_SendSFR( uint16_t total_bytes, uint8_t *p_source_addr ,uint8_t mod) {//SFR mode for 1~32 bytes data
// p_source_addr: point source data buffer start address, set NULL if copy into PIOC buffer already
	if ( total_bytes > RGB1W_SFR_SIZE ) return;
	PIOC->D8_SYS_CFG = RB_MST_RESET | RB_MST_IO_EN0| RB_MST_IO_EN1;//clear&halt PIOC
	PIOC->D8_SYS_CFG = RB_MST_CLK_GATE | RB_MST_IO_EN0| RB_MST_IO_EN1;//run PIOC before write SFR
	if ( p_source_addr ) memcpy( RGB1W_SFR_ADDR, p_source_addr, total_bytes );//copy source data to RGB1W SFR, @PIOC run
	if(mod)
	    RGB1W_COMMAND = ((uint8_t)total_bytes|0X40);// PIOC start send
	else
	    RGB1W_COMMAND = (uint8_t)total_bytes;// PIOC start send

}

/*********************************************************************
 * @fn      RGB1W_SendRAM
 *
 * @brief   RAM mode for 1~3072 bytes data.
 *
 * @param   total_bytes - total data number(byte).
 *          p_source_addr - data.
 *          mod - 0:PC18 or PC7
 *                1:PC19
 *
 * @return  none
 */
void RGB1W_SendRAM( uint16_t total_bytes, uint8_t *p_source_addr ,uint8_t mod) {//RAM mode for 1~3072 bytes data
// p_source_addr: point source data buffer start address, set NULL if copy into PIOC buffer already
	if ( total_bytes > RGB1W_RAM_SIZE ) return;
	PIOC->D8_SYS_CFG = RB_MST_RESET | RB_MST_IO_EN0| RB_MST_IO_EN1;//clear&halt PIOC
	if ( p_source_addr ) memcpy( RGB1W_RAM_ADDR, p_source_addr, total_bytes );//copy source data to PIOC SRAM, @PIOC halt
	PIOC->D8_SYS_CFG = RB_MST_CLK_GATE | RB_MST_IO_EN0| RB_MST_IO_EN1;//run PIOC after load data in SRAM
	PIOC->D16_DATA_REG0_1 = total_bytes;// data size
	if(mod)
	    PIOC->D8_DATA_REG2 = 1;
	else
	    PIOC->D8_DATA_REG2 = 0;

#if defined SYSCLK_FREQ_24MHz_HSI
	RGB1W_COMMAND = RGB1W_CYC_24M | RGB1W_CMD_RAM;// set bit cycle and PIOC start send
#else	// default is 48MHz
	RGB1W_COMMAND = RGB1W_CYC_48M | RGB1W_CMD_RAM;// set bit cycle and PIOC start send
#endif

}

/*********************************************************************
 * @fn      RGB1W_SendSFR_Wait
 *
 * @brief   SFR mode for 1~3072 bytes data wait PIOC operate finish.
 *
 * @param   total_bytes - total data number(byte).
 *          p_source_addr - data.
 *          mod - 0:PC18 or PC7
 *                1:PC19
 * @return  none
 */
uint8_t RGB1W_SendSFR_Wait( uint16_t total_bytes, uint8_t *p_source_addr ,uint8_t mod) {//SFR mode for 1~32 bytes data
// p_source_addr: point source data buffer start address, set NULL if copy into PIOC buffer already
	if ( total_bytes == 0 || total_bytes > RGB1W_SFR_SIZE ) return( RGB1W_ERR_PARA );
	RGB1W_SendSFR( total_bytes, p_source_addr , mod);
	while ( ( PIOC->D8_SYS_CFG & RB_INT_REQ ) == 0 );//wait, PIOC request interrupt after finish
	return( PIOC->D8_CTRL_RD );//auto remove interrupt request after reading
}

/*********************************************************************
 * @fn      RGB1W_SendRAM_Wait
 *
 * @brief   RAM mode for 1~3072 bytes data wait PIOC operate finish.
 *
 * @param   total_bytes - total data number(byte).
 *          p_source_addr - data.
 *          mod - 0:PC18 or PC7
 *                1:PC19
 * @return  none
 */
uint8_t RGB1W_SendRAM_Wait( uint16_t total_bytes, uint8_t *p_source_addr ,uint8_t mod) {//RAM mode for 1~3072 bytes data
// p_source_addr: point source data buffer start address, set NULL if copy into PIOC buffer already
	if ( total_bytes == 0 || total_bytes > RGB1W_RAM_SIZE ) return( RGB1W_ERR_PARA );
	RGB1W_SendRAM( total_bytes, p_source_addr ,mod);
	while ( ( PIOC->D8_SYS_CFG & RB_INT_REQ ) == 0 );//wait, PIOC request interrupt after finish
	return( PIOC->D8_CTRL_RD );//auto remove interrupt request after reading
}

/*********************************************************************
 * @fn      RGB1W_Halt
 *
 * @brief   halt/sleep PIOC .
 *
 * @return  none
 */
void RGB1W_Halt( void ) {//halt/sleep PIOC
	PIOC->D8_SYS_CFG &= ~ RB_MST_CLK_GATE;
}

